import { getData, updateData } from '@vimi/utils-api'
import { SERVICE_NAME } from '@/credit/plugins/constant'

const state = {
  entrustContract: {},
}

const getters = {
  entrustContract: state => state.entrustContract,
}

const actions = {
  async getEntrustContract({ commit }, relatedId) {
    if (!relatedId) {
      return commit('entrustContract', {})
    }

    const res = await getData(SERVICE_NAME, 'entrust_contract', {
      'customer_related_id.eq': relatedId,
    })

    commit('entrustContract', (res && res.data && res.data.length && res.data[0]) || {})
  },

  async updateEntrustContract({ _ }, { id, data }) {
    return updateData(SERVICE_NAME, 'entrust_contract', id, data)
  },
}

const mutations = {
  entrustContract: (state, payload) => (state.entrustContract = payload),
}

export default {
  state,
  getters,
  actions,
  mutations,
}
